﻿using System;
using System.Linq;
using System.Linq.Dynamic;
using ChoViet.Lib.Paging;
using System.Collections.Generic;

namespace ChoViet.Model
{
    public class AdManager
    {
        public static IPagedList<Ad> GetListAd(int? demandId, int? areaId, int? cityId, int? districtId, int? custypeId, int? cateId, string keyword, bool activeYn, bool pubYn, string order, string orderDir, int? page, int? pageSize, int? userId)
        {
            ChoVietDataContext eDataContext = new ChoVietDataContext();
            try
            {
                if (eDataContext.Connection.State == System.Data.ConnectionState.Closed) eDataContext.Connection.Open();
                var ads = eDataContext.Ads
                    .Where(a => demandId > 0 ? a.DemandID == demandId : true)
                    .Where(a => areaId > 0 ? a.AreaID == areaId : true)
                    .Where(a => cityId > 0 ? a.CityID == cityId : true)
                    .Where(a => districtId > 0 ? a.DistrictID == districtId : true)
                    .Where(a => custypeId > 0 ? a.CusTypeID == custypeId : true)
                    .Where(a => cateId > 0 ? (a.CatID == cateId|| a.Category.ParentID == cateId) : true)
                    .Where(a => userId.Value > 0 ? a.UserID == userId : true)
                   // .Where(a => activeYn == true ? a.ActiveYN == activeYn : true)
                    .Where(a => pubYn == true ? a.PublishYN == pubYn : true)
                    .Where(a => keyword != null && keyword.Trim().Length >0 ? a.Title.ToLower().Contains(keyword.ToLower()) : true)
                    .OrderBy(string.Format("{0} {1}", order, orderDir))
                    .ToPagedList(page.Value - 1, pageSize.Value);
                return ads;
            }
            catch (Exception)
            {
                eDataContext.Connection.Dispose();
                return null;
            }
        }

        public static IPagedList<Ad> GetListAd(int? demandId,int ? countryId, int? areaId, int? cityId, int? districtId, int? custypeId, int? cateId, string keyword, bool activeYn, bool pubYn, string order, string orderDir, int? page, int? pageSize, int? userId)
        {
            ChoVietDataContext eDataContext = new ChoVietDataContext();
            try
            {
                if (eDataContext.Connection.State == System.Data.ConnectionState.Closed) eDataContext.Connection.Open();
                var ads = eDataContext.Ads
                    .Where(a => demandId > 0 ? a.DemandID == demandId : true)
                    .Where(a => countryId > 0 ? a.CountryID == countryId : true)
                    .Where(a => areaId > 0 ? a.AreaID == areaId : true)
                    .Where(a => cityId > 0 ? a.CityID == cityId : true)
                    .Where(a => districtId > 0 ? a.DistrictID == districtId : true)
                    .Where(a => custypeId > 0 ? a.CusTypeID == custypeId : true)
                    .Where(a => cateId > 0 ? (a.CatID == cateId || a.Category.ParentID == cateId) : true)
                    .Where(a => userId.Value > 0 ? a.UserID == userId : true)
                    .Where(a => pubYn == true ? a.PublishYN == pubYn : true)
                    .Where(a => keyword != null && keyword.Trim().Length > 0 ? a.Title.ToLower().Contains(keyword.ToLower()) : true)
                    .OrderBy(string.Format("{0} {1}", order, orderDir))
                    .ToPagedList(page.Value - 1, pageSize.Value);
                return ads;
            }
            catch (Exception)
            {
                eDataContext.Connection.Dispose();
                return null;
            }
        }
        
        public static List<Category> GetChildCategory(int catid)
        {
            try
            {
                var db = new ChoVietDataContext();
                var returnVal = new List<Category>();
                var curentCat = db.Categories.FirstOrDefault(o => o.ID == catid);
                returnVal.Add(curentCat);

                foreach (var item in curentCat.Categories.ToList())
                {
                    returnVal.AddRange(GetChildCategory(item.ID));
                }

                return returnVal;
            }
            catch (Exception exp)
            {
                return null;
            }
        }

        public static List<Category> GetChildCategory(Category category)
        {
            try
            {
                var db = new ChoVietDataContext();
                var returnVal = new List<Category>();
                //var curentCat = db.Categories.FirstOrDefault(o => o.ID == catid);
                returnVal.Add(category);

                foreach (var item in category.Categories.ToList())
                {
                    returnVal.AddRange(GetChildCategory(item));
                }

                return returnVal;
            }
            catch (Exception exp)
            {
                return null;
            }
        }

        //public static List<Category> GetRootCategories(int demandID)
        //{
        //    var dataContext = new ChoVietDataContext();
        //    return dataContext
        //        .Categories
        //        .Where(e => e.ParentID == null)
        //        .OrderBy(e => e.ID)
        //        .ToList();
        //}

        public static Category GetCategory(int id)
        {
            var dataContext = new ChoVietDataContext();
            return dataContext.Categories.SingleOrDefault(row => row.ID == id);
        }
        public static Category GetCategoryByShortName(string shortName)
        {
            var dataContext = new ChoVietDataContext();
            return dataContext.Categories.SingleOrDefault(row => row.ShortName == shortName);
        }
        public static int GetTotalNewAdInDay() {
            var dataContext = new ChoVietDataContext();
            var total = dataContext.Ads.Where(e => e.PublishYN == true && e.CreatedDate.Value.Day == DateTime.Now.Day).Count();
            if (total <= 10) total += 10;
            return total;
        }
        public static List<Tag> GetTagsByAdId(int adId)
        {
            if (adId <= 0) return null;
            ChoVietDataContext eDataContext = new ChoVietDataContext();
            var listtag = eDataContext.AdTagMaps.Where(e => e.AdID == adId).Select(e => e.Tag).ToList();
            return listtag;
        }
        public static List<Ad> GetListAdByUser(User user)
        {
            ChoVietDataContext eDataContext = new ChoVietDataContext();
            var lst = eDataContext.Ads.Where(e => e.UserID == user.ID || e.EmailContact == user.Email).OrderByDescending(e => e.CreatedDate).ToList();
            if (lst.Count() > 0) return lst;
            else return null;
        }
    }
}
